<?php
class TimechecksController extends AppController {

	var $name = 'Timechecks';

	function index() {
		$this->Timecheck->recursive = 0;
		$this->set('timechecks', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid timecheck', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('timecheck', $this->Timecheck->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Timecheck->create();
			if ($this->Timecheck->save($this->data)) {
				$this->Session->setFlash(__('The timecheck has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The timecheck could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid timecheck', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Timecheck->save($this->data)) {
				$this->Session->setFlash(__('The timecheck has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The timecheck could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Timecheck->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for timecheck', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Timecheck->delete($id)) {
			$this->Session->setFlash(__('Timecheck deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Timecheck was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	/**
	 * 
	 * 
	 * @param array $info
	 * @return unknown|number|string
	 */
	function checkTime (array $info){
		$score=$info['score'];
		$timecheck_id=$info['timecheck_id'];
		$submitted_time=$info['submitted_time'];
		
		$timecheck=$this->Timecheck->findById ($timecheck_id);
		$type=$timecheck['Timecheck']['timecheck_type'];
		$time_num1=$timecheck['Timecheck']['time_number1'];
		$tim_num2=$timecheck['Timecheck']['time_number2'];	
		if ($type=='TRI'){
			if ($submitted_time<$time_num1) return $score;
			else return 0;
		}
		if ($type=='REC'){
			return $score*(1-$submitted_time/$time_num1);
		}
		if ($type=='TRAP'){
			if ($submitted_time<$time_num1) return score;
			else return $score*(1-($submitted_time-$time_num1)/$time_num2);
		}
	}
}
